<%@page import="java.util.UUID"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/pages/common/taglib.jsp" %>
<%
    String uuid = UUID.randomUUID().toString();
    request.setAttribute("uuid", uuid);
%>
<style>
.title{
	padding-top: 30px;
    padding-bottom: 20px;
    margin-bottom:20px;
    font-size: 24px;
    text-align: left;
    border-bottom: 1px solid gray; 
}
.btns{
	border-bottom:  1px solid #DDDDDD; 
	border-top:  1px solid #DDDDDD; 
	background-color:#EEEEEE;
  	height:40px;
  	text-align:right;
  	padding:5px;
}
</style>
<div class="container-fluid" id="answerView">
	<div class="title" id="title">
		
	</div>
	<ol class="subjects">
		
	</ol>
	<div class="btns">
		<a class="btn btn-sm btn-success am-margin-left-xs submitBtn" title="提交"><span class="glyphicon glyphicon-plus"></span> 提交</a>
	</div>
</div>
<script type="text/javascript">
$(function(){
	var id='${id}';
	var answerView=$("#answerView");
	var subjects=answerView.find(".subjects");
	var subjectInputs=[];
	$.getJSON(ctx+"/oaExamAct/loadSubjects.do",{id:id},function(d){
		$("#title").html(d.TITLE);
		$.each(d.subjects,function(i,s){
			var subject=$("<li>"+
					"<div>"+s.TITLE+"</div>"
					+"<div class='options'></div>"
					+"</li>")
			subjects.append(subject);
			var options=subject.find(".options");

			var sType=s.TYPE;
			var sTypeStr="";
			if(sType==1){
				sTypeStr="radio";
			}else if(sType==2){
				sTypeStr="checkbox";
			}
			var name="sub_"+s.ID;
			subjectInputs.push({
				name:name,
				value:"",
				subjectId:s.ID,
				sType:sType
			});
			$.each(s.options,function(oi,op){
				var oDom=op.TITLE+"&nbsp;&nbsp;<input type='"+sTypeStr+"' name='"+name+"' value='"+op.VALUE+"'>&nbsp;&nbsp;&nbsp;&nbsp;";
				options.append(oDom);
			});
		});
	});
	
	$(".submitBtn").click(function(){
		var hasCheck=false;
		$.each(subjectInputs,function(i,s){
			var vs=[]
				$("input[name="+s.name+"]:checked").each(function(i,n){
					vs.push($(n).val());
				});
			if(vs && vs.length>0){
				s.value=vs.join(",");
			}else{
				alert("请对全部题目进行答题后再提交试卷");
				hasCheck=false;
				return false;
			}
			hasCheck=true;
		});
		if(hasCheck){
			$.post(ctx+"/oaExamAct/submitAnswer.do",{examId:id,datas:Base64.encode(JSON.stringify(subjectInputs))},function(d){
				if(d.success){
				}else{
					webUtils.error(d.msg);
				}
			},"json")
		}
	});
});
</script>